<?php
header('Content-Type: application/json');
if (isset($_POST['pid'])) {
    include_once("db_connection.php");
    $conn = getdb();
    $stmt = $conn->prepare("SELECT  pname, price  FROM products WHERE pid = :pid");
    $stmt->bindParam(':pid', $_POST['pid']);
    $stmt->execute();
    $result = $stmt->setFetchMode(PDO::FETCH_ASSOC);
    $current_product = $stmt->fetch();
    if ($current_product == null) {
        echo json_encode(array('failed' => 'product is off the shelf'));
    } else {
        echo json_encode(array('success' => array("price" => $current_product['price'], "name" => $current_product['pname'])));
    }
} else {
    echo json_encode(array('failed' => 'missing parameter pid'));
}
